<template>
	<view>
		<!-- 功能列表 -->
		<u-collapse :border="showBorder">

		</u-collapse>
		<u-line></u-line>

		<u-cell class="cell" :border="showBorder"  title="播放器设置" @click="showPlayer = !showPlayer"
			icon="play-right-fill" size="large" iconStyle="color: #ff8763" :isLink="true"></u-cell>
		<view style="display: flex;flex-direction: row; align-items: center;padding-bottom: 10px;" v-if="showPlayer">
			<view style="padding-left: 50px;padding-right: 20px;"> 开启H5播放器:</view>
			<u-switch activeColor="#ff8763" v-model="player" @change="changePlayer"></u-switch>
		</view>
		<u-line></u-line>


		<!-- 检查push过程未结束不显示，push设置项 -->
		<u-cell class="cell" :border="showBorder" :value="storageSize" title="清理缓存" @click="showClear = true"
			icon="trash-fill" size="large" iconStyle="color: #ff8763" :isLink="true"></u-cell>
		<u-line></u-line>
		<!--
		<u-cell class="cell" :border="showBorder" v-if="supportMode.includes('fingerPrint')" title="指纹解锁"
			@click.native="startSoterAuthentication('fingerPrint')"icon="fingerprint" size="large" iconStyle="color: #ff8763" :isLink="true">
		</u-cell>
		<u-line></u-line>
		<u-cell class="cell" :border="showBorder" v-if="supportMode.includes('facial')" title="人脸解锁"
			@click="startSoterAuthentication('facial')" icon="../../static/user/l5.png"  size="large" iconStyle="color: #ff8763" :isLink="true"></u-cell>
		<u-line></u-line>	 -->

		<u-cell :border="showBorder" class="cell" title="检测更新" icon="reload" size="large" iconStyle="color: #ff8763"
			@click="checkUpdate" :isLink="true"></u-cell>
		<u-line></u-line>
		<u-cell :border="showBorder" class="cell" title="关于我们" icon="tags-fill" size="large" iconStyle="color: #ff8763"
			url="/pages/about/about" :isLink="true"></u-cell>
		<u-line></u-line>
		</u-cell-group>
		<!-- 退出/登录 按钮 -->
		<view class="bottom-back" v-if="hasLogin" @click="show = true">
			<text class="bottom-back-text">退出登录</text>
		</view>
		<view class="bottom-back" v-else @click="login">
			<text class="bottom-back-text">登录</text>
		</view>
		<u-modal :show="show" :showCancelButton="true" @cancel="show=false" title="提示" content='确定退出登录吗?'
			@confirm="logout"></u-modal>
		<u-modal :show="showClear" :showCancelButton="true" @cancel="showClear=false" title="提示" content='确定清除缓存吗?'
			@confirm="clearStorage"></u-modal>

	</view>
</template>

<script>
	import {
		checkAndUpdate,
		initData
	} from '../../config/util.js';

	export default {
		data() {
			return {
				supportMode: [],
				userInfo: {},
				hasLogin: false,
				showBorder: false,
				show: false,
				showClear: false,
				storageSize: '',
				player: false,
				showPlayer: false,
				baseUrl:'http://192.168.2.105:9999',
				showGateWay: false,
			}
		},
		onLoad() {
			this.player = uni.getStorageSync('playerSet') ==1 ? true : false
			this.getStorageSize();
			if (uni.getStorageSync('userId')) {
				this.hasLogin = true;
			} else {
				this.hasLogin = false;

			}
			// #ifdef APP-PLUS || MP-WEIXIN
			uni.checkIsSupportSoterAuthentication({
				success: (res) => {
					console.log(res);
					this.supportMode = res.supportMode
				},
				fail: (err) => {
					console.log(err);
				}
			})
			// #endif
		},
		onShow() {

		},
		methods: {
			changePlayer(value) {
				if (value) {
					uni.setStorageSync('playerSet',1)
				} else {
					uni.setStorageSync('playerSet',0)
				}
			},
			toEdit() {
				uni.navigateTo({
					url: '/pages/ucenter/userinfo/userinfo'
				});
			},
			changePwd() {
				uni.navigateTo({
					url: '/pages/ucenter/login-page/pwd-retrieve/pwd-retrieve?phoneNumber=' +
						(this.userInfo && this.userInfo.mobile ? this.userInfo.mobile : ''),
					fail: err => {
						console.log(err);
					}
				});
			},
			/**
			 * 开始生物认证
			 */
			startSoterAuthentication(checkAuthMode) {
				console.log(checkAuthMode);
				let title = {
					"fingerPrint": "指纹解锁",
					"facial": "人脸解锁"
				} [checkAuthMode]
				// 检查是否开启认证
				this.checkIsSoterEnrolledInDevice({
						checkAuthMode,
						title
					})
					.then(() => {
						console.log(checkAuthMode, title);
						// 开始认证
						uni.startSoterAuthentication({
							requestAuthModes: [checkAuthMode],
							challenge: '123456', // 微信端挑战因子
							authContent: `请用${title}`,
							complete: (res) => {
								console.log(res);
							},
							success: (res) => {
								console.log(res);
								if (res.errCode == 0) {
									/**
									 * 验证成功后开启自己的业务逻辑
									 *
									 * app端以此为依据 验证成功
									 *
									 * 微信小程序需要再次通过后台验证resultJSON与resultJSONSignature获取最终结果
									 */
									return uni.showToast({
										title: `${title}成功`,
										icon: 'none'
									});
								}
								uni.showToast({
									title: '认证失败请重试',
									icon: 'none'
								});
							},
							fail: (err) => {
								console.log(err);
								console.log(`认证失败:${err.errCode}`);
								uni.showToast({
									title: `认证失败`,
									icon: 'none'
								});
							}
						})
					})
			},
			checkIsSoterEnrolledInDevice({
				checkAuthMode,
				title
			}) {
				return new Promise((resolve, reject) => {
					uni.checkIsSoterEnrolledInDevice({
						checkAuthMode,
						success: (res) => {
							console.log(res);
							if (res.isEnrolled) {
								return resolve(res);
							}
							uni.showToast({
								title: `设备未开启${title}`,
								icon: 'none'
							});
							reject(res);
						},
						fail: (err) => {
							console.log(err);
							uni.showToast({
								title: `${title}失败`,
								icon: 'none'
							});
							reject(err);
						}
					})
				})
			},
			login() {
				uni.navigateTo({
					url: '../login/login'
				})

			},
			logout() {
				this.$store.commit('SetToken', '');
				this.$store.commit('SetUserId', '');
				this.$store.commit('SetUser', '');
				uni.reLaunch({
					url: "../my/my"
				})
			},
			getStorageSize: function() {
				let that = this;
				uni.getStorageInfo({
					success(res) {
						//console.log(res.keys);
						//console.log(res.limitSize);
						let size = res.currentSize;
						if (size < 1024) {
							that.storageSize = size + ' B';
						} else if (size / 1024 >= 1 && size / 1024 / 1024 < 1) {
							that.storageSize = Math.floor(size / 1024 * 100) / 100 + ' KB';
						} else if (size / 1024 / 1024 >= 1) {
							that.storageSize = Math.floor(size / 1024 / 1024 * 100) / 100 + ' M';
						}
					}
				})
			},
			clearStorage: function() {
				uni.clearStorageSync('SubNavList');
				uni.clearStorageSync('SubNavList0');
				uni.clearStorageSync('SubNavList1');
				uni.clearStorageSync('SubNavList2');
				uni.clearStorageSync('SubNavList3');
				uni.clearStorageSync('SubNavList4');
				uni.clearStorageSync('SubNavList5');
				uni.clearStorageSync('SubNavList6');
				//重新获取并显示清除后的缓存大小
				this.getStorageSize();
				this.showClear= false
				initData()
			},
			checkUpdate() {
				checkAndUpdate(true);
			},
			gateWayChange(val){
				this.baseUrl = val
			},
			updateGateWay(){
				uni.setStorageSync('BASE_URL',this.baseUrl);
			}
		}
	}
</script>

<style class="scss">
	page {
		background-color: #e7e7e7,
	}

	.u-collapse-item,
	.cell {
		padding: 5px 0px 5px 20px;
		background-color: #FFFFFF;
	}

	.bottom-back {
		margin-top: 10px;
		height: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		/* #ifndef APP-NVUE */
		border: none;
		/* #endif */
		border-width: 0;
		border-radius: 5px;
		margin-left: 5px;
		margin-right: 5px;

		background: linear-gradient(to right, #ff8763, #ff6364);
	}

	.bottom-back-text {
		font-size: 33rpx;
	}
</style>
